package cn.com.elite;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.DocumentException;

import dnapay.common.Strings;
import dnapay.common.ToolKit;
import dnapay.service.client.PosMessage;
import dnapay.service.client.TransactionClient;
import dnapay.service.client.TransactionType;

public class HTPayThread implements Runnable{
	private static final Log log=LogFactory.getLog(HTPayThread.class);
	private boolean flag=true; //停止线程标记 
	public static int HZ=5;
	public String chaxun_sql1="";
	public String chaxun_sql2="";
	public String chaxun_sql3="";
	public String chaxun_sql4="";
	
	public static int getHZ() {
		return HZ;
	}
	public static void setHZ(int hz) {
		HZ = hz;
	}
	public String getChaxun_sql1() {
		return chaxun_sql1;
	}
	public void setChaxun_sql1(String chaxun_sql1) {
		this.chaxun_sql1 = chaxun_sql1;
	}
	public String getChaxun_sql2() {
		return chaxun_sql2;
	}
	public void setChaxun_sql2(String chaxun_sql2) {
		this.chaxun_sql2 = chaxun_sql2;
	}
	public String getChaxun_sql3() {
		return chaxun_sql3;
	}
	public void setChaxun_sql3(String chaxun_sql3) {
		this.chaxun_sql3 = chaxun_sql3;
	}
	public String getChaxun_sql4() {
		return chaxun_sql4;
	}
	public void setChaxun_sql4(String chaxun_sql4) {
		this.chaxun_sql4 = chaxun_sql4;
	}
	
	private String getSerialNO() {
        return dnapay.common.DateFormatter.HHmmss(new java.util.Date());
    }
	private void PayChaxun_YL(String sql,TransactionClient tm){
		log.info("sql:"+sql);
		List list=HTPayService.ebean.chaxun_sql(sql);
		if(list!=null&&list.size()>0){
			for(int i=0;i<list.size();i++){
				Map map=(Map)list.get(i);
				String merorderno=(String)map.get("merorderno");
				log.info("merorderno:"+merorderno);
				try {
					PosMessage pm=tm.orderQuery(getSerialNO(),"",merorderno,Strings.random(24));
					log.info("PayChaxun_YL_pm:"+pm);
					String zhifu_result=pm.getRemark();
					log.info("zhifu_result："+zhifu_result);
					dorun2(zhifu_result,merorderno);
				}catch (Exception e) {
					log.info("PayChaxun_YL:"+Util.getERRO(e));
				}
				
			}
		}
		
	}
	private void dorun2(String zhifu_result,String merorderno){
		String COM_CODE=HTPayService.ebean.chaxunString(" select HTPay_Getagency('"+merorderno+"') from dual");
		log.info("COM_CODE："+COM_CODE);
		if(zhifu_result.indexOf("0000")!=-1){
			log.info("zhifu_result:"+zhifu_result);
			HTPayService.getJyCgProcess(merorderno,"",COM_CODE,HTPayService.USER,HTPayService.PASSWORD);
		}else if(zhifu_result.indexOf("T451")!=-1){
			log.info("zhifu_result:"+zhifu_result);
			log.info("T451,继续等待");
		}else{
			log.info("zhifu_result:"+zhifu_result);
			HTPayService.getJyCxProcess(merorderno,"",COM_CODE,HTPayService.USER,HTPayService.PASSWORD);
		}

	}
	private void PayChaxun_ZT(String sql){
		log.info("sql:"+sql);
		List list=HTPayService.ebean.chaxun_sql(sql);
		if(list!=null&&list.size()>0){
			for(int i=0;i<list.size();i++){
				Map map=(Map)list.get(i);
				String merorderno=(String)map.get("merorderno");
				log.info("merorderno:"+merorderno);
				String zhifu_result=(String)map.get("respcode");
				log.info("zhifu_result:"+zhifu_result);
				try {
					dorun2(zhifu_result,merorderno);
					HTPayService.ebean.update_sql("update htpay_table set trytime=trytime+1 where merorderno='"+merorderno+"'");
				}catch (Exception e) {
					log.info("PayChaxun_YL:"+Util.getERRO(e));
				}
			}
		}
	}
	private void dorun(){
		TransactionClient tm=new TransactionClient(HTPayService.WSDL,"");
	    tm.setTransactionType(TransactionType.CA);
		tm.setServerCert(ToolKit.getPropertyFromFile(HTPayService.Key)); 
		System.setProperty("javax.net.ssl.trustStore", ToolKit.getPropertyFromFile("javax.net.ssl.trustStore"));
	    System.setProperty("javax.net.ssl.trustStorePassword", ToolKit.getPropertyFromFile("javax.net.ssl.trustStorePassword"));
	    tm.setMerchantNo(HTPayService.MerchantType+HTPayService.MerchantNo);
	    tm.setMerchantPassWD(HTPayService.MerchantPassWD);
	    tm.setTerminalNo(HTPayService.TerminalNo);
		PayChaxun_YL(chaxun_sql1,tm);
		PayChaxun_YL(chaxun_sql2,tm);
		PayChaxun_YL(chaxun_sql3,tm);
		PayChaxun_ZT(chaxun_sql4);
	}
	public void run(){
		log.info("-------------HTPayThread-------------------------------");
		while(flag){
			try{
				dorun();
			}catch(Exception e){
				log.error("run():"+Util.getERRO(e));
			}
			try{
				log.debug("时间未到，休眠59分");
            	Thread.sleep(HZ*1000);
            }catch(Exception e){
            	log.error("定时错误:"+Util.getERRO(e)); 
            }
		}
	}
}
